/*
 * Copyright 2009 Johnson Consulting Services
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.johnsoncs.gwt.widgets.samples.client;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.johnsoncs.gwt.widgets.client.common.events.BusyStateChangeEvent;
import com.johnsoncs.gwt.widgets.client.common.events.BusyStateChangeHandler;
import com.johnsoncs.gwt.widgets.client.common.events.BusyStateChangeEvent.BusyState;
import com.johnsoncs.gwt.widgets.client.dynamictable.DynamicTable;
import com.johnsoncs.gwt.widgets.client.dynamictable.Selection;
import com.johnsoncs.gwt.widgets.client.dynamictable.TableModel;
import com.johnsoncs.gwt.widgets.client.dynamictable.Selection.SelectionType;
import com.johnsoncs.gwt.widgets.client.dynamictable.events.ModelChangeEvent;
import com.johnsoncs.gwt.widgets.client.dynamictable.events.ModelChangeHandler;
import com.johnsoncs.gwt.widgets.client.dynamictable.events.RowFocusEvent;
import com.johnsoncs.gwt.widgets.client.dynamictable.events.RowFocusHandler;

public class TableStatusWidget<T extends Object> extends Composite {
	private final FlexTable grid = new FlexTable();
	private int rowCount = 0;
	public TableStatusWidget(DynamicTable<T> table) {
		super.initWidget(grid);
		
		final Sequence idx = new Sequence();
		
		TableModel<T> model = table.getModel();
		model.addBusyStateChangeHandler(new BusyStateChangeHandler() {
			final int col = idx.next();
			@Override
			public void onStateChange(BusyStateChangeEvent event) {
				grid.setText(0, col, "Model is: " + ((event.getState() == BusyState.BUSY) ? "busy" : "idle"));
			}
		});
		model.addModelChangeHandler(new ModelChangeHandler() {
			final int col = idx.next();
			@Override
			public void onRowCountChange(ModelChangeEvent event) {
				rowCount = event.getRowCount();
				grid.setText(0, col, "Row count: " + rowCount);
			}
		});
		model.addSelectionHandler(new SelectionHandler<Selection<T>>() {
			final int col = idx.next();
			@Override
			public void onSelection(SelectionEvent<Selection<T>> event) {
				Selection<T> sel = event.getSelectedItem();
				int count = 0;
				if (sel.getType() == SelectionType.EXCLUSIVE) {
					count = rowCount - sel.getIds().size();
				} else {
					count = sel.getIds().size();
				}
				grid.setText(0, col, "Selected row count: " + count);
				
			}
		});
		table.addRowFocusHandler(new RowFocusHandler<T>() {
			{
				grid.setText(1, 0, "No row currently focused");
				grid.getFlexCellFormatter().setColSpan(1, 0, idx.current());
			}
			@Override
			public void onRowFocus(RowFocusEvent<T> event) {
				if (event.getFocusedValue() == null) {
					grid.setText(1, 0, "No row currently focused");					
				} else {
					grid.setText(1, 0, "Focused row: " + event.getFocusedValue().toString());
				}
			}
			
		});
	}
	
	private class Sequence {
		private int index;
		
		public int next() {
			return index++;
		}
		public int current() {
			return index;
		}
	}
}
